﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BLL.U8.CommonMethod
{
    public class DataConverter
    {
        /// <summary>
        /// 将DataTable转换为对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> ConvertDataTable<T>(DataTable dt)
        {
            List<T> data = new List<T>();
            foreach (DataRow row in dt.Rows)
            {
                T item = GetItem<T>(row);
                data.Add(item);
            }
            return data;
        }
        private static T GetItem<T>(DataRow dr)
        {
            Type temp = typeof(T);
            T obj = Activator.CreateInstance<T>();

            foreach (DataColumn column in dr.Table.Columns)
            {
                foreach (PropertyInfo pro in temp.GetProperties())
                {
                    if (pro.Name == column.ColumnName)
                    {
                        var value = dr[column.ColumnName];

                        if (value == DBNull.Value)
                        {
                            pro.SetValue(obj, null, null);
                        }
                        else
                        {
                            pro.SetValue(obj, dr[column.ColumnName], null);
                        }
                    }
                    else
                        continue;
                }
            }
            return obj;
        }

        ///// <summary>
        ///// 为GridView的列名赋值, 仅当字符串列表数量与表格视图的列数量相等时有效
        ///// </summary>
        ///// <param name="gView">表格视图</param>
        ///// <param name="captions">字符串列表</param>
        //public static GridView NamingGridColumn(GridView gView, List<string> captions)
        //{
        //    if (gView.Columns.Count() == captions.Count())
        //    {
        //        for (int i = 0; i < captions.Count(); i++)
        //        {
        //            var col = gView.Columns[i];
        //            col.Caption = captions[i];
        //        }
        //    }

        //    return gView;

        //}

        /// <summary>
        /// 将新列表添加到原始列表中，规则：原始列表中特定字符串属性，与新列表中值相同的对象被删除，再加入新列表对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="listA">原始列表</param>
        /// <param name="listB">新列表</param>
        /// <param name="propertyName">字符串属性的名称</param>
        public static void UpdateList<T>(List<T> listA, List<T> listB, string propertyName, bool IsAppend = false)
        {
            var dulplicated = listA.Where(aL => listB.Any(
                bL =>
                bL.GetType().GetProperty(propertyName).GetValue(bL, null).ToString()
                ==
                aL.GetType().GetProperty(propertyName).GetValue(aL, null).ToString()
                ));

            listA.RemoveAll(a => dulplicated.Any(
                d =>
                d.GetType().GetProperty(propertyName).GetValue(d, null).ToString()
                ==
                a.GetType().GetProperty(propertyName).GetValue(a, null).ToString()
                ));

            if (!IsAppend)
            {
                listA.InsertRange(0, listB);
            }
            else
            {
                listA.AddRange(listB);
            }
        }
    }
}
